package com.ykdl.member.kid.gears;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LayoutAnimationController;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.ArticlesAdapter;
import com.ykdl.member.kid.beans.ArticleBean;
import com.ykdl.member.kid.beans.ArticleListBean;
import com.ykdl.member.kid.gears.ArticlesActivity.getDataTag;
import com.ykdl.member.kid.xlist.XListView;
import com.ykdl.member.kid.xlist.XListView.IXListViewListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class ArticlesActivity_Demo extends BaseScreen implements IXListViewListener{

	private XListView articles_list;
	private ArticlesAdapter adapter;
	private int cursor = 0;
	private int count = 20;
	private Animation animation;
	private LayoutAnimationController controller;

//	private ArrayList<ArticleBean> datalist = new ArrayList<ArticleBean>();
	private boolean isGoneBt = false;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("育儿汇", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {

				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.articlesactivity_xml);
		inint();
	}

	public void inint() {
		adapter = new ArticlesAdapter(this);
		articles_list = (XListView) findViewById(R.id.articles_list);
		articles_list.setPullLoadEnable(true);
		// 允许下拉
		articles_list.setPullRefreshEnable(true);
		// 设置监听器
		articles_list.setXListViewListener(this);
		articles_list.gonefoot(true);
		articles_list.setAdapter(adapter);
		articles_list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ArticleBean bean = (ArticleBean) adapter.getItem(arg2);
				Intent intent = new Intent(ArticlesActivity_Demo.this,
						ArticlesWebviewActivity.class);
				intent.putExtra(KidAction.ARTICLE_ID, bean.getArticle_id() + "");
				ArticlesActivity_Demo.this.startActivity(intent);
			}

		});
		inintdata();
	}
	public void setANmation(){
		
	}

	public void inintdata() {
		String url = KidConfig.ARTICLES_INDEX;
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(url, map, null);
		TaskManager.startHttpRequest(request, new getDataTag(),
				ArticleListBean.class);
	}

	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ArticleListBean) {
				ArticleListBean bean = (ArticleListBean) t;
				if (null != bean.getArticles()) {
					if (bean.getArticles().size() > 0) {
						articles_list.setVisibility(View.VISIBLE);
						adapter.setList(bean.getArticles());
//						for (ArticleBean obj : bean.getArticles()) {
//							datalist.add(obj);
//						}
//						adapter.setList(datalist);
						if (bean.getNext_cursor() == bean.getTotal_number()) {
							isGoneBt = true;
							articles_list.stopLoadMore();
//							articles_list.gonefoot(false);
							articles_list.gonefoot();  
						} else {
//							articles_list.gonefoot(true);
							cursor = bean.getNext_cursor();
						}
						
						animation = new AlphaAnimation(0f, 1f);
						animation.setDuration(500);
						controller = new LayoutAnimationController(animation, 1f);
						controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
						articles_list.setLayoutAnimation(controller);
						
						adapter.notifyDataSetChanged();
						articles_list.stopRefresh();
					} else {
						showErrorTip("", "没有数据");
						articles_list.setVisibility(View.GONE);
					}
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			articles_list.stopRefresh();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub

		}

	}

	@Override
	public void onRefresh() {
//		datalist.clear();
		if(cursor>0){
			cursor=cursor-count;
		}else{
			cursor=0;
		}
		isGoneBt=false;
		inintdata();
	}

	@Override
	public void onLoadMore() {
		if (isGoneBt == false) {
			inintdata();
		}else{
			articles_list.stopLoadMore();
			articles_list.stopRefresh();
		}
	}


}
